<extend name="layout/main" />

<block name="HEADER">
	<style type="text/css">
		.xw__grid .layui-table-view .layui-table td .layui-table-cell{
			height: 50px;
		    line-height: 50px;
		}
		.file-img {
		    height: 100%;
		    background-position: left;
		    background-size: 100% 85%;
		    background-repeat: no-repeat;
		    cursor: pointer;
		}
		#detailedInfoForm .xw-form-formType-div{
			padding: 0 !important;
			padding-left: 5px !important;
		}
		#detailedInfoForm .sections:last-child .table-cell:last-child{ margin-bottom: 0; }
	</style>
</block>

<block name="CONTENT">
	<div class="wrapper">
		<div class="panel-heading">
			<control:search id="searchs" />
		</div>
		<div class="content" style="height: calc(100% - 102px) !important;">
			<div style="height: 100%;width:calc(100% - 400px);float: left;">
				<control:grid id="grid" padding="0" height="100%" />
			</div>
			<div style="width: 400px;float: left;" id="detailedInfoForm"></div>
		</div>
	</div>

</block>

<block name="FOOTER">
	<script type="text/javascript">
		var page = (function(ns) {

			var detailedInfoForm = null;
			
			//名称模板
		    ns.gridNameTemplet= function(e){
		    	var val = e.value;
		    	var str = 
		    		"<div  style='line-height:24px;' onclick='page.openGoods()'>"
			    		+ "<p class='link' style='overflow: hidden;text-overflow: ellipsis;white-space: nowrap;' title='"+val+"'>"
			    			+val
			    		+"</p>"
			    		+ "<p style='font-size: 13px;color:#e24444;'>￥"+ $.toDecimal(Number(e.data.goods_sale_price)) +
			    			(function(){
			    				var s = '';
					    			s="<span style='text-decoration:line-through;color: #a7a7a7;margin-left:10px;'>￥"
					    				+$.toDecimal(Number(e.data.goods_oid_price))
					    			+"</span>";
			    				return s;
			    			}())
			    		+ "</p>";
		    		+"</div>";
		    	return str;
		    }
			
			//订单时间模板
			ns.gridTimeTemplet = function(e) {
				return "<div style='line-height:24px;white-space: normal;'>" + e.data.create_time + "</div>";
			}

			//地址模板
			ns.gridAddressTemplet = function(e) {
				var str =
					"<div style='line-height:24px;'>" +
					"<p>" + e.data.user_name + "&nbsp;&nbsp;" + e.data.user_phone + "</p>" +
					"<p>" + e.data.user_address+ "</p>" +
					"</div>";
				return str;
			}

			//页面初始化
			ns.init = function() {
				ns.initForm();
				ns.query();
				grid.loadSuccess(function() {
					ns.setstatusNum();
				});
			}

			//设置状态数量
			ns.setstatusNum = function(data) {
				var param = $.map(grid.$filter, function(h) {
					if(h.field != 'status') return h;
				});
				$.ajaxs("/admin/v1/Order/getAllOrderStatus", JSON.stringify(param), function(res) {
					$.each(searchs.$el.find('input[name="status2"]'), function(index, h) {
						var count = res.data ? (res.data[$(this).val()] || 0) : 0;
						$(this).next('.layui-form-radio').find('div').find('span').remove();
						if(index > 0 && count) {
							$(this).next('.layui-form-radio').find('div')
								.append("<span style='color:#D9522D;line-height: 1;'>(" + count + ")</span>");
						}
					});
					//全部
					var allCount = eval($.map(res.data, function(h, key) {
						if([10, 11, 12, 13].indexOf(Number(key)) == -1) return h || 0;
					}).join('+') || 0);
					if(allCount)
						searchs.$el.find('input[name="status2"]').eq(0).next('.layui-form-radio').find('div')
						.append("<span style='color:#D9522D;line-height: 1;'>(" + allCount + ")</span>");
				},false);
			}

			//搜索事件
			ns.query = function() {
				var filter = $.map(searchs.getData(), function(h) {
					return h;
				});
				grid.load(filter);
			}

			//初始化from
			ns.initForm = function() {
				detailedInfoForm = layui.forms.render("detailedInfoForm", "", {
					mode: 1,
					oid: ''
				});
			}

			//行单击事件
			ns.rowClick = function() {
				var selectedRow = grid.getSelectedRows()[0];
				detailedInfoForm.setFormValue(selectedRow);
				detailedInfoForm.$grids['goodsGrid'].load([{
					opera: "=",
					field: "order_id",
					value: selectedRow['id']
				}])
			}

			//订单详情
			ns.edit = function() {
				var selectedRow = grid.getSelectedRows()[0];
				var filter = {
					url: "modules/order/list/edit",
					oid: selectedRow.id,
					mode: 2
				}
				$.openIframe(filter, "订单详情", ['1050px', '582px']);
			}

			//发货
			ns.deliverGoods = function() {
				if(!grid.isSelected()) {
					return false;
				}
				var selectedRow = grid.getSelectedRows()[0];
				var filter = {
					url: "modules/order/list/deliver",
					oid: selectedRow.id,
					mode: 2
				}
				$.openIframe(filter, "订单发货", ['600px', '480px']);
			}
			
			//退款详情
			ns.refundDetails = function() {
				if(!grid.isSelected()) {
					return false;
				}
				var selectedRow = grid.getSelectedRows()[0];
				var filter = {
					url: "modules/order/list/refundDetails",
					oid: selectedRow.id,
					mode: 2
				}
				$.openIframe(filter, "退款详情", ['600px', '480px']);
			}
			
			//打开商品
			ns.openGoods = function() {
				setTimeout(function(){
					var selectedRow = detailedInfoForm.$grids['goodsGrid'].getSelectedRows()[0];
					var filter = {
						url: "modules/goods/goodsList/edit",
						oid: selectedRow.goods_id,
						mode: 3
					}
					$.openIframe(filter, "商品详情", ['720px', 'calc(100% - 50px)']);
				},100);
			}

			//打印
			ns.printing = function() {
				if(!grid.isSelected()) {
					return false;
				}
			}

			return ns;
		}({}));

		$(function() { page.init(); });
	</script>
</block>